package com.fortune.service.impl;


import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.fortune.dal.dao.BillInfoDAO;
import com.fortune.dal.model.BillInfo;
import com.fortune.service.BillInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BillInfoServiceImpl implements BillInfoService {

    @Autowired
    private BillInfoDAO billInfoDAO;

    @Override
    public void addBillInfo(BillInfo billInfo) throws BusinessException {
        try {
            billInfoDAO.insert(billInfo);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

    @Override
    public void modifyBillInfo(BillInfo billInfo) throws BusinessException {
        try {
            billInfoDAO.updateById(billInfo);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

    @Override
    public void deleteBillInfo(Integer debtBillId) throws BusinessException {
        try{
            billInfoDAO.deleteById(debtBillId) ;
        }catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

    @Override
    public BillInfo queryBillInfo(Integer debtBillId) throws BusinessException {
        BillInfo c = null;
        try {
            c = billInfoDAO.queryById(debtBillId);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return c;
    }

    @Override
    public List<BillInfo> queryBillInfoList(BillInfo billInfo) throws BusinessException {
        List<BillInfo> list = null;
        try {
            list = billInfoDAO.queryByModel(billInfo);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return list;
    }

    @Override
    public BillInfo queryCreditCardBillInfo(Integer cardId, String billDate) throws BusinessException {
        BillInfo param = new BillInfo();
        param.setType("1");
        param.setIssuerId(cardId);
        param.setBillDate(billDate);
        List<BillInfo> billInfos = queryBillInfoList(param) ;
        if(ListUtils.isEmpty(billInfos))
            return null;
        return billInfos.get(0);
    }
}
